const { getMediaMeta, getFramesLenth, getFrames } = require('./js/ffmpeg.js')
const path = require('path')

//#region 
function wsfb(wss, callback) {
    wss.on('connection', ws => {

        const send = data => ws.send(JSON.stringify({
            msg: data,
            code: 0
        }))

        const error = (data, code = 1) => ws.send(JSON.stringify({
            msg: data,
            code: code
        }))

        // 监听消息事件
        ws.on('message', message => callback(JSON.parse(message.toString()), send, error))

        // 监听关闭事件
        ws.on('close', () => console.log(`\x1B[47m ${wss.options.port} \x1B[0m WebSocket连接关闭`))
    })
}
//#endregion


const wsf = {
    // 命令
    test: wss => wsfb(wss, (msg, send, error) => {
        const header = {
            response: [msg?.type, msg.request ? '-' + msg.request : ''].join(''),
            token: msg?.token,
            time: Date.now(),
        }
        sendmsg = (data, code = 0) => {
            send({ code, data, header })
        }

        switch (msg.type) {
            case 'check':
                sendmsg('ok')
                break;

            case 'media':
                const mediaPath = msg.param.mediaPath
                const internalPath = msg.param.internalPath
                const cmdPath = internalPath => {
                    return {
                        ffmpeg: path.resolve(internalPath, 'ffmpeg/ffmpeg.exe'),
                        ffplay: path.resolve(internalPath, 'ffmpeg/ffplay.exe'),
                        ffprobe: path.resolve(internalPath, 'ffmpeg/ffprobe.exe')
                    }
                }

                switch (msg.request) {
                    case 'getBaseInfo':
                        getMediaMeta(mediaPath, cmdPath(internalPath), msg.debug)
                            .then(metadata => {
                                console.log('获取视频参数', mediaPath);

                                sendmsg(metadata)
                            })
                            .catch(err => {
                                console.error(err);
                            })
                        break;

                    case 'getFramesLenth':
                        getFramesLenth(mediaPath, cmdPath(internalPath), msg.debug)
                            .then(metadata => {
                                console.log('获取视频帧数', mediaPath);

                                sendmsg(metadata)
                            })
                            .catch(err => {
                                console.error(err);
                            })
                        break


                    case 'getFrames':
                        getFrames(mediaPath, cmdPath(internalPath), msg.param.start, msg.debug)
                            .then(frames => {
                                sendmsg(frames)
                            })
                            .catch(err => {
                                console.error(err);
                            })
                        break

                    default:
                        break;
                }


                break

            default:
                sendmsg('无匹配')
                break;
        }
    })
}



module.exports = { wsf, wsfb }